package service

import (
	"bytes"
	"github.com/faiface/beep"
	"github.com/faiface/beep/speaker"
	"github.com/faiface/beep/wav"
	"log"
	"time"
)

func PlayAudio(data []byte) {

	musicResource := bytes.NewReader(data)
	streamer, format, err := wav.Decode(musicResource)
	if err != nil {
		log.Fatalln("mp3解析错误：", err)
	}
	defer streamer.Close()

	speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))

	done := make(chan bool)
	speaker.Play(beep.Seq(streamer, beep.Callback(func() {
		done <- true
	})))
	<-done

}
